﻿using Microsoft.AspNetCore.Mvc;
using EasyIot.WebApp.Common;
using EasyIot.Model;
using EasyIot.Service;
using EasyIot.Common;

namespace EasyIot.WebApp.Controllers
{
    [HandlerLogin]
    public class BaseController : Controller
    {
        protected const string SuccessText = "操作成功！";

        protected const string ErrorText = "操作失败！";

        static OperatorModel mOperatorModel = null;

        public OperatorModel Operator
        {
            get 
            { 
                if (null == mOperatorModel) mOperatorModel = new OperatorProvider(HttpContext).GetCurrent();
                return mOperatorModel;
            }
            set => Operator = value;
        } 

        public virtual ActionResult Index(int? id)
        {
            if (null != Operator)
            {
                var _menuId = id == null ? 0 : id.Value;
                var _roleId = Operator.RoleId;

                if (id != null)
                {
                    string menyKey = _roleId + "_" + _menuId;

                    if (!Operator.DicRightButtonList.ContainsKey(menyKey)) Operator.DicRightButtonList.Add(menyKey, ButtonService.GetButtonListByRoleIdModuleId(_roleId, _menuId, PositionEnum.FormInside));
                    if (!Operator.DicTopButtonList.ContainsKey(menyKey)) Operator.DicTopButtonList.Add(menyKey, ButtonService.GetButtonListByRoleIdModuleId(_roleId, _menuId, PositionEnum.FormRightTop)); 

                    ViewData["RightButtonList"] = Operator.DicRightButtonList[menyKey];
                    ViewData["TopButtonList"] = Operator.DicTopButtonList[menyKey];
                }
            }
            return View();
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual AjaxResult SuccessTip(string message = SuccessText)
        {
            return new AjaxResult { state = ResultType.success.ToString(), message = message };
        }

        /// <summary>
        /// 操作失败
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        protected virtual AjaxResult ErrorTip(string message = ErrorText)
        {
            return new AjaxResult { state = ResultType.error.ToString(), message = message };
        }
    }
}